#include "conf.h"

#include <stdlib.h>

#include "log/log.h"

struct config {
    struct {
        char ip[20];
        int port;
    } server;
};

struct config config = { 0 };

void load_default_config(struct config *conf)
{
}

int load_conf(const char *path)
{
    char errbuf[200];

    FILE *fp = fopen(path, "r");
    if (!fp) {
        log_warn("open config: %s\n", path);
        return -1;
    }

    toml_table_t *tbl = toml_parse_file(fp, errbuf, sizeof(errbuf));
    if (!tbl)
        log_fatal("toml parse: %s\n", errbuf);

    toml_value_t host = toml_table_string(tbl, "host");
    toml_value_t port = toml_table_int(tbl, "port");

    if (!host.ok)
        host.u.s = "localhost";

    if (!port.ok)
        port.u.i = 80;

    config.server.port = port.u.i;

    toml_free(tbl);
    return 0;
}

inline struct config *get_conf(void)
{
    return &config;
}
